package com.baicaoyuan.web.service;

import com.baicaoyuan.web.model.StockApplyDO;
import com.baicaoyuan.web.model.StockInDO;
import com.baicaoyuan.web.model.UserDO;
import com.baicaoyuan.web.repository.StockApplyRepository;
import com.baicaoyuan.web.repository.StockInRepository;
import com.baicaoyuan.web.repository.UserRepository;
import com.baicaoyuan.web.request.br.BStockApplyRequest;
import com.baicaoyuan.web.response.StockApplyResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Service
public class StockApplyService {

    @Autowired
    StockApplyRepository stockApplyRepository;

    @Autowired
    StockInRepository stockInRepository;

    @Autowired
    UserRepository userRepository;

    public List<StockApplyResponse> getStockApply(Integer page, Integer size, String status) {
        List<StockApplyDO> list = this.stockApplyRepository.getStockApply((page - 1) * size, size, status);
        List<StockApplyResponse> result = new ArrayList<>();
        list.forEach(x -> {
            StockApplyResponse r = new StockApplyResponse();
            BeanUtils.copyProperties(x, r);
            result.add(r);
        });

        return result;
    }

    public void save(BStockApplyRequest request) {
        StockApplyDO sa = new StockApplyDO();
        sa.setUserId(request.getUserId());
        sa.setName(request.getName());
        sa.setStockNumber(request.getStockNumber());
        sa.setImages(request.getImages());
        Objects.requireNonNull(this.stockApplyRepository.save(sa));
    }

    public Integer getCountOfAll(String status) {
        return this.stockApplyRepository.countAllByStatusIs(status);
    }

    /**
     * 审核如果通过的话，则记录入股信息
     *
     * @param id
     * @param status
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStatus(Long id, String status) {
        try {
            Integer r = this.stockApplyRepository.updateStatus(id, status);

            //如果审核通过，则添加入股记录
            if ("2".equals(status) && r > 0) {
                StockApplyDO apply = this.stockApplyRepository.getOne(id);
                StockInDO in = new StockInDO();
                in.setUserId(apply.getUserId());
                in.setStockApplyId(apply.getId());
                in.setStockNumber(apply.getStockNumber());
                stockInRepository.save(in);

                UserDO u = userRepository.getOne(apply.getUserId());
                u.setCategory("3");
                userRepository.save(u);
            } else {
                throw new SQLException();
            }
            return r;
        } catch (Exception ex) {
            System.out.println("方法回滚");
        }
        return 0;
    }
}

